﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Net;
using System.ComponentModel;
using System.Resources;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.Reflection;


    public class updateApplication
    {
        private string DEFAULTURL = "http://test.candasys.co.uk/";

        public string setURL
        {
            get { return DEFAULTURL; }
            set { DEFAULTURL = value; }
        }

        public bool checkVersion(Version curVersion)
        {
            Version newVersion = this.getVersion();

            if (curVersion.CompareTo(newVersion) < 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /* Format of XML file used to check for update
    <?xml version="1.0" encoding="utf-8"?>
    <applicationdata>
    <version>1.0.0.2</version>
    <updateurl>http://test.candasys.co.uk/deployments/candasupport/setup.exe</updateurl>
    </applicationdata>
        */
        public bool hasUpdated()
        {
            try
            {
                RegistryKey regKey = Registry.CurrentUser;
                regKey = regKey.CreateSubKey(Application.UserAppDataRegistry.ToString());

                if (regKey.GetValue("isupdating") != null)
                {
                    if (regKey.GetValue("isupdating").ToString() == "1")
                    {
                        Console.WriteLine("Updated");
                        regKey.SetValue("isupdating", "0");
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                return false;
            }
        }
        public string showChangeLog()
        {
            string HTML = "";
            IWebProxy proxy = WebRequest.GetSystemWebProxy();
            proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            using (var client = new WebClient())
            {
                client.Proxy = proxy;
                client.Credentials = proxy.Credentials;                
                HTML = client.DownloadString(DEFAULTURL + "deployments/swiftoolreportmanager/updatelist.php");
            }

            return HTML;
        }

        public Version getVersion()
        {
            Version newVersion = null;
            String downloadurl = null;

            httpPostData http = new httpPostData(DEFAULTURL + "deployments/swiftoolreportmanager/checkversion.php");
            
            var xmlresponse = http.GetResponse();


            if (xmlresponse.Length > 0)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlresponse);
                newVersion = new Version(xmlDoc.SelectSingleNode("/applicationdata/version").InnerText);
                downloadurl = xmlDoc.SelectSingleNode("/applicationdata/updateurl").InnerText.ToString();
                
                RegistryKey regKey = Registry.CurrentUser;
                regKey = regKey.CreateSubKey(Application.UserAppDataRegistry.ToString());
                regKey.SetValue("newversion", newVersion);
                regKey.SetValue("downloadurl", downloadurl);
            }
            else
            {
                MessageBox.Show("Unable to check for updates, please check your internet connection.");
            }
            return newVersion;
        }
    }

